package m202401.d05;

import java.util.Stack;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/5 09:11
 */
//20. 有效的括号
public class Q20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
                continue;
            }
            if (stack.isEmpty()) {
                return false;
            }
            Character pop = stack.pop();
            if (c == ')' && pop == '(') {
                continue;
            }
            if (c == ']' && pop == '[') {
                continue;
            }
            if (c == '}' && pop == '{') {
                continue;
            }
            return false;
        }
        return stack.isEmpty();
    }
}
